package com.snow.useviewdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019/5/13
 * desc   :
 */
public class MoreLineTextView extends View {
    private Canvas canvas;
    private Paint mPaint;
    private PorterDuffXfermode xformode;

    public MoreLineTextView(Context context) {
        super(context);
        init();
    }

    public MoreLineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MoreLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(50);
        mPaint.setTextSize(100);
        mPaint.setColor(Color.parseColor("#00ff00"));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;
        drawText();
    }

    private float postIndex;

    private void drawRed() {
        xformode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        Bitmap srcBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas srcCanvas = new Canvas(srcBitmap);
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        float lineHeight = -top + bottom;
        srcCanvas.drawText(striings[0], 0, lineHeight, mPaint);
        mPaint.setXfermode(xformode);
        mPaint.setColor(Color.RED);
        RectF rectF = new RectF(0, 0, postIndex, getMeasuredHeight());
        srcCanvas.drawRect(rectF, mPaint);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        init();
        if (postIndex < 100) {
            postIndex += 10;
        } else {
            postIndex = 0;
        }
        mhandler.sendEmptyMessage(101);
    }
    Handler mhandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    mhandler.sendEmptyMessageDelayed(101,80);
                    break;
            }
            postInvalidate();
            return false;
        }
    });

    String[] striings;

    public void drawText() {
        striings = new String[]{"床前明月光，", "疑是地上霜。", "举头望明月，", "低头思故乡。"};
        Point point = new Point(0, 0);
        textCenter(striings, mPaint, canvas, point, Paint.Align.LEFT);
    }

    private int curLine;

    private void textCenter(String[] strings, Paint paint, Canvas canvas, Point point, Paint.Align aligin) {
        curLine = 0;
        paint.setTextAlign(aligin);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        int length = strings.length;
        float lineHeight = -top + bottom;
        for (int i = 0; i < length; i++) {
            curLine++;
            canvas.drawText(strings[i], point.x, point.y + curLine * lineHeight, paint);
        }
    }

    public void drawTwo() {
        drawRed();
//        String[] striings = {"床前明月光，", "疑是地上霜。", "举头望明月，", "低头思故乡。"};
//        mPaint.setColor(Color.parseColor("#ff0000"));
//        Point point = new Point(0, 0);
//        textCenter(striings, mPaint, canvas, point, Paint.Align.LEFT);
    }
}